package com.hmsm.sys.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.servlet.ModelAndView;

import com.hmsm.sys.controller.LoginController;
import com.lzyyj.common.BeanUtils;
import com.lzyyj.gmis.base.service.IUserService;
import com.lzyyj.gmis.base.webui.ICurrentUserAware;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 当前用户检查拦截器，对请求的当前用户进行检查
 * 1、将当前用户信息注入线程变量池
 * 2、如果Action需要，将用户对象注入Action
 * @author Lzyyj
 */
@SuppressWarnings("serial")
public class CurrentUserCheckInterceptor extends AbstractInterceptor {
	
	private static Log log = LogFactory.getLog(CurrentUserCheckInterceptor.class);

	@Resource
	private LoginController loginController;

	public void setLoginController(LoginController loginController){
		this.loginController = loginController;
	}
	
	public LoginController getLoginController() {
		return this.loginController;
	}
	
	private IUserService<?,?> userService;

	public IUserService<?,?> getUserService() {
		return userService;
	}

	public void setUserService(IUserService<?,?> userService) {
		this.userService = userService;
	}

	/**
	 * session 中当前用户 key
	 */
	private String currentUserKey = "CurrentUser";

	public String getCurrentUserKey() {
		return currentUserKey;
	}

	public void setCurrentUserKey(String currentUserKey) {
		this.currentUserKey = currentUserKey;
	}

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		Map<String, Object> session = ai.getInvocationContext().getSession();

		Object currentUser = session.get(currentUserKey);

		// 获取HttpServletRequest对象
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// 检查检查腾讯通登录令牌
		String userName =  request.getParameter("userName");
		String syncData =  request.getParameter("syncData");
		String syncurl = request.getParameter("syncurl");
		if(syncurl==null){
			// 瑞宏升统一登录
			if(userName!=null&&userName.length()>0&&syncData!=null&&syncData.length()>12){
				ModelAndView modelAndView = loginController.rhsLogin(userName, syncData, syncurl, request);
				Object user = modelAndView.getModel().get("currentUser");
				
				log.info(userName+"换令牌！");
			}
		}else{
			// 本系统同步登录
			if(userName!=null&&userName.length()>0&&syncData!=null&&syncData.length()>12){
				ModelAndView modelAndView = loginController.syncLogin(userName, syncData, syncurl, request);
				Object user = modelAndView.getModel().get("currentUser");
				
				log.info(userName+"换令牌！");
			}
		}
		
		// 如果有注入兼容的userService，创建线程变量并保存当前用户信息
		if(userService!=null){
			Map<String, Object> map = userService.getThreadLocalMap(true);
			if (map!=null){
				map.put(userService.getCurrentUserKey(), currentUser);
			}
		}

		Object action = ai.getAction();
	
		// 如果已登录用户，并且 action 要求注入当前用户
		if (currentUser != null && action instanceof ICurrentUserAware) {
			// 判断用户对象类型与Action要求注入的类型是否匹配
			BeanUtils.setProperty(action, "currentUser", currentUser);
		}
		return ai.invoke();
	}
	
}